package com.itxialu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itxialu.reggie.common.R;
import com.itxialu.reggie.dto.DishDto;
import com.itxialu.reggie.dto.SetmealDto;
import com.itxialu.reggie.entity.Category;
import com.itxialu.reggie.entity.Dish;
import com.itxialu.reggie.entity.Setmeal;
import com.itxialu.reggie.service.CategoryService;
import com.itxialu.reggie.service.SetmealDishService;
import com.itxialu.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐
 *
 * @author shkstart
 * @create 2022-11-02 23:56
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired//套餐
    private SetmealService setmealService;

    @Autowired//套餐菜品关系
    private SetmealDishService setmealDishService;

    @Autowired//菜品分类
    private CategoryService categoryService;

    //新增套餐
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {//提交过来的是JSON格式的数据
        log.info("新增套餐信息{}", setmealDto);
        //新增套餐，同时保存套餐和菜品的关联关系
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功！");
    }

    //套餐分页查询
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {
        Page<Setmeal> SetmealPage = new Page<>(page, pageSize);
        Page<SetmealDto> SetmealDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //添加过滤条件,like:模糊查询
        //wrapper.like(name != null,Dish::getName,name);
        //添加排序
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        //执行分页查询
        setmealService.page(SetmealPage, wrapper);

        //对象拷贝,忽略records属性(原始对象，新对象,忽略字段)
        BeanUtils.copyProperties(SetmealPage, SetmealDtoPage, "records");

        //把categoryName菜品名称放进去
        List<Setmeal> records = SetmealPage.getRecords();
        List<SetmealDto> list = records.stream().map((item) -> {//
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);

            Long categoryId = item.getCategoryId();//拿到每个菜品的分类id
            Category category = categoryService.getById(categoryId);//拿到一个分类对象
            String categoryName = category.getName();//得到分类名称
            setmealDto.setCategoryName(categoryName);

            return setmealDto;
        }).collect(Collectors.toList());//最后收集起来转成集合

        SetmealDtoPage.setRecords(list);

        return R.success(SetmealDtoPage);
    }

    //修改套餐-->数据回显
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable long id) {
        log.info("修改套餐-->数据回显，需要回显的套餐ID{}", id);
        SetmealDto setmealDto = setmealService.getByIdWithFlavor(id);
        return R.success(setmealDto);
    }

    //更新/修改菜品
    @PutMapping//前端提交JSON数据要加@RequestBody注解
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        log.info("套餐管理，套餐更新");
        setmealService.updateWithFlavor(setmealDto);////新增菜品，同时保存口味数据
        return R.success("菜品更新成功！");
    }

    //停售、启售
    @PostMapping("/status/{ids}")
    public R<String> status(long ids) {
        Setmeal byId = setmealService.getById(ids);
        System.out.println("停售、启售修改..." + ids + "拿到实体" + byId);

        Integer status = byId.getStatus();
        if (status != 0) {
            System.out.println("0停售");
            byId.setStatus(0);
            setmealService.updateById(byId);
        } else {
            System.out.println("1起售");
            byId.setStatus(1);
            setmealService.updateById(byId);
        }
        return R.success("修改成功！");
    }

    //删除套餐
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        System.out.println("开始删除套餐ID" + ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功！");
    }

    //商务套餐列表
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();//条件构造器

        //设置条件
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(Setmeal::getStatus, setmeal.getStatus());
        //排序
        wrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> setmealList = setmealService.list(wrapper);
        return R.success(setmealList);
    }

}
